package com.mrx.pvzcommunity.controller;

import com.mrx.pvzcommunity.model.Result;
import com.mrx.pvzcommunity.model.thread.Comment;
import com.mrx.pvzcommunity.model.thread.UserThread;
import com.mrx.pvzcommunity.model.user.User;
import com.mrx.pvzcommunity.service.CommentService;
import com.mrx.pvzcommunity.service.UserThreadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Api(tags = "帖子操作")
@RequestMapping(value = "/threads")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")

public class UserThreadController {

    private final UserThreadService userThreadService;

    private final CommentService commentService;

    @GetMapping("/{tid}")
    @ApiOperation(value = "帖子查询", notes = "查指定 id 的帖子")
    @ApiImplicitParam(name = "tid", value = "帖子 id", dataTypeClass = Integer.class, required = true, example = "1")

    public Result<List<UserThread>> getThreads(@PathVariable String tid) {
        return userThreadService.findThreadsByTid(tid);
    }

    @GetMapping("/comments/{tid}")
    @ApiOperation(value = "帖子评论查询", notes = "查指定 id 帖子的评论")
    @ApiImplicitParam(name = "tid", value = "帖子 id", dataTypeClass = Integer.class, required = true, example = "1")

    public Result<List<Comment>> getComments(@PathVariable String tid) {
        return commentService.findCommentByTid(tid);
    }

    @PostMapping("/comments/{tid}")
    @ApiOperation(value = "发布评论", notes = "给指定 id 帖子发评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tid", value = "帖子 id", dataTypeClass = Integer.class, required = true, example = "1"),
            @ApiImplicitParam(name = "content", value = "评论内容", dataTypeClass = String.class, required = true, example = "1")
    })
    public Result<String> publishComment(
            @PathVariable String tid, @ApiIgnore @SessionAttribute(value = "user", required = false) User user,
            @RequestBody String content
    ) {
        return commentService.addCommentByTid(tid, user, content);
    }

}
